package com.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.AuditRecordsDto;
import com.platform.system.domain.entity.chip.AuditRecords;
import com.platform.system.domain.vo.chip.AuditRecordsVo;
import com.platform.system.mapper.chip.AuditRecordsMapper;
import com.platform.system.mapper.auth.SysUserMapper;
import com.platform.system.service.ISysAuditRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */
@Service
@Slf4j
public class SysAuditRecordsServiceImpl extends ServiceImpl<AuditRecordsMapper, AuditRecords> implements ISysAuditRecordsService {

    @Resource
    AuditRecordsMapper auditRecordsMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertAuditRecords(AuditRecordsVo auditRecordsVo) {
        AuditRecords auditRecords = new AuditRecords();
        auditRecords.setTenantUid(auditRecordsVo.getTenantUid());
        auditRecords.setResourceType(auditRecordsVo.getResourceType());
        auditRecords.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        auditRecords.setAuditUserId(auditRecordsVo.getAuditUserId());
        auditRecords.setAuditStatus(auditRecordsVo.getAuditStatus());
        auditRecords.setResourceUid(auditRecordsVo.getResourceUid());
        auditRecords.setUserId(auditRecordsVo.getUserId());
        auditRecords.setAuditTime(auditRecordsVo.getAuditTime());
        auditRecords.setRejectReason(auditRecordsVo.getRejectReason());
        auditRecords.setAuditType(auditRecordsVo.getAuditType());
        auditRecords.insert();
        return AjaxResult.success();
    }

    @Override
    public TableDataInfo selectAuditRecordsList(AuditRecordsVo auditRecordsVo) {
        LambdaQueryWrapper<AuditRecords> recordsLambdaQueryWrapper = new QueryWrapper<AuditRecords>().lambda();
        recordsLambdaQueryWrapper.eq(AuditRecords::getTenantUid, auditRecordsVo.getTenantUid());
        recordsLambdaQueryWrapper.eq(AuditRecords::getResourceType, auditRecordsVo.getResourceType());
        recordsLambdaQueryWrapper.eq(StringUtils.isNotNull(auditRecordsVo.getAuditStatus()), AuditRecords::getAuditStatus, auditRecordsVo.getAuditStatus());
        recordsLambdaQueryWrapper.orderByAsc(AuditRecords::getAuditStatus);
        recordsLambdaQueryWrapper.orderByDesc(AuditRecords::getCreateTime);
        Page<AuditRecords> page = new Page<>();
        page.setCurrent(auditRecordsVo.getPageNum());
        page.setSize(auditRecordsVo.getPageSize());
        IPage<AuditRecords> recordsIPage = auditRecordsMapper.selectPage(page, recordsLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        List<AuditRecords> list =  recordsIPage.getRecords();
        List<AuditRecordsDto> auditRecordsDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            list.forEach(auditRecords -> {
                AuditRecordsDto auditRecordsDto = new AuditRecordsDto();
                BeanUtils.copyProperties(auditRecords, auditRecordsDto);
                //提交审批的用户名
                Long userId = auditRecords.getUserId();
                SysUser user = sysUserMapper.selectUserById(userId);
                auditRecordsDto.setUserName(user.getUserName());
                //审批名称
                if(auditRecords.getAuditUserId() != null){
                    SysUser getAuditUser = sysUserMapper.selectUserById(auditRecords.getAuditUserId());
                    auditRecordsDto.setAuditUserName(getAuditUser.getUserName());
                }
                auditRecordsDtoList.add(auditRecordsDto);
            });
        }
        rspData.setRows(auditRecordsDtoList);
        rspData.setTotal(recordsIPage.getTotal());
        return rspData;
    }

    @Override
    public AjaxResult updateAuditRecords(AuditRecordsVo auditRecordsVo) {
        LambdaUpdateWrapper<AuditRecords> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AuditRecords::getResourceUid, auditRecordsVo.getResourceUid());
        AuditRecords auditRecords = new AuditRecords();
        auditRecords.setAuditTime(new Date());
        BeanUtils.copyProperties(auditRecordsVo, auditRecords);
        auditRecords.update(lambdaUpdateWrapper);
        return AjaxResult.success();
    }
}
